/*
 * Created by Christopher King
 * Copyright (C) 2007 Christopher King, All Rights Reserved.
 */
package us.cirion.adventure.engine;

import java.util.Enumeration;
import java.util.Vector;

/**
 * ItemLocation describes a place or thing that can hold items. Examples include
 * a room or a character's inventory.
 * 
 * @author Chris
 */
public abstract class ItemLocation
{

	private Vector items;

	/**
	 * Construct a new, empty ItemLocation.
	 */
	public ItemLocation()
	{
		items = new Vector();
	}

	/**
	 * Insert the provided item into this location. Because each item can only
	 * be in one place at a time, adding it here will remove it from any
	 * previous location.
	 * 
	 * @param item The item to place here.
	 */
	public void addItem(Noun item)
	{
		// Don't add special items; these will end up hooking back into this
		// function with the actual location.
		if (!(item instanceof AnyNoun))
		{
			if (!items.contains(item))
			{
				items.addElement(item);
			}
		}
		ItemLocation currentLocation = item.getLocation();
		if (currentLocation == null || !currentLocation.equals(this))
		{
			item.setLocation(this);
		}
	}

	/**
	 * Retrieves an unsorted collection of all the items at this location.
	 * 
	 * @return The items at this location.
	 */
	public Enumeration getItems()
	{
		return items.elements();
	}

	/**
	 * Remove the indicated item from this location.
	 * 
	 * @param item The item to remove.
	 */
	public void removeItem(Noun item)
	{
		items.removeElement(item);
	}

	/**
	 * Reports whether the provided item is at this location.
	 * 
	 * @param item The item to query.
	 * @return <code>true</code> if the item is here, <code>false</code>
	 *         otherwise.
	 */
	public boolean containsItem(Noun item)
	{
		return items.contains(item);
	}
}
